package com.ruoyi.crop.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.crop.api.domain.Grow;
import com.ruoyi.crop.service.GrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 农作物生长Controller
 *
 * @author ruoyi
 * @date 2024-07-23
 */
@RestController
@RequestMapping("/grow")
public class GrowController extends BaseController
{
    @Autowired
    private GrowService growService;

    /**
     * 查询农作物生长列表
     */
    @GetMapping("/list/{cropCode}")
    public AjaxResult list(@PathVariable(value = "cropCode") String cropCode)
    {
        startPage();
        List<Grow> list = growService.selectGrowList(cropCode);
        return success(list);
    }

    /**
     * 获取农作物生长详细信息
     */
    @RequiresPermissions("system:grow:query")
    @GetMapping(value = "/{growId}")
    public AjaxResult getInfo(@PathVariable("growId") String growId)
    {
        return success(growService.getById(growId));
    }

    /**
     * 新增农作物生长
     */
    @RequiresPermissions("system:grow:add")
    @Log(title = "农作物生长", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Grow grow)
    {
        grow.setCreateBy(SecurityUtils.getUsername());
        grow.setCreateTime(new Date());
        return toAjax(growService.save(grow));
    }

    /**
     * 修改农作物生长
     */
    @RequiresPermissions("system:grow:edit")
    @Log(title = "农作物生长", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Grow grow)
    {
        return toAjax(growService.updateById(grow));
    }

    /**
     * 删除农作物生长
     */
    @RequiresPermissions("system:grow:remove")
    @Log(title = "农作物生长", businessType = BusinessType.DELETE)
	@DeleteMapping("/{growIds}")
    public AjaxResult remove(@PathVariable String[] growIds)
    {
        return toAjax(growService.removeBatchByIds(Arrays.asList(growIds)));
    }
}
